﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using Jc.MicroDesigner.Management;
using System.Reflection;
using System.Globalization;

namespace Jc.MicroDesigner.ToolWindows
{
    /// <summary>
    /// Поле для открытия формы редактирования несвязной с ним информации
    /// </summary>
    public class CustomPropertyField
    {
        public Type _FormType
        {
            get;
            set;
        }

        public ObjectManager _Manager
        {
            get;
            set;
        }

        public string _Title
        {
            get;
            set;
        }
    }

    /// <summary>
    /// Редактор свойств с вызовом требуемой формы
    /// </summary>
    public class CustomPropertyEditor : UITypeEditor
    {
        /// <summary>
        /// Реализация метода редактирования
        /// </summary>
        public override Object EditValue(
          ITypeDescriptorContext context,
          IServiceProvider provider,
          Object value)
        {
            if ((context != null) && (provider != null))
            {
                IWindowsFormsEditorService svc =
                  (IWindowsFormsEditorService)
                  provider.GetService(typeof(IWindowsFormsEditorService));

                if (svc != null)
                {
                    if (value != null)
                    {
                        Type ft = (value as CustomPropertyField)._FormType;
                        ConstructorInfo x = ft.GetConstructor(new Type[] { });
                        Form f = (Form)x.Invoke(new Type[] { });
                        MethodInfo mi = ft.GetMethod("Show", new Type[] { typeof(ObjectManager) });
                        if ((value as CustomPropertyField)._Manager != null)
                            mi.Invoke(f, new object[] { (value as CustomPropertyField)._Manager });
                    }
                }
            }

            return base.EditValue(context, provider, value);
        }

        /// <summary>
        /// Возвращаем стиль редактора - модальное окно
        /// </summary>
        public override UITypeEditorEditStyle GetEditStyle(
          ITypeDescriptorContext context)
        {
            if (context != null)
                return UITypeEditorEditStyle.Modal;
            else
                return base.GetEditStyle(context);
        }
    }

    /// <summary>
    /// Конвертер для свойств
    /// </summary>
    public class CustomPropertyConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context,
          CultureInfo culture,
          object value,
          Type destType)
        {
            return (value as CustomPropertyField)._Title;
        }

        public override object ConvertFrom(ITypeDescriptorContext context,
          CultureInfo culture,
          object value)
        {
            return false;
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            // false - можно вводить вручную
            // true - только выбор из списка
            return true;
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return true;
            return base.CanConvertTo(context, destinationType);
        }
    }
}